<?php
if (!defined('IN_DISCUZ'))
	die('Access Denied');

global $_G;

$row = $_G['product'];
$provice = provice();

//var_dump($row);

if (!$row || $row['hide'] || !$row['available']){
	showmessage('product_is_not_available', NULL, array(), array(
			'login'=>1
	));
}
//////////////////////////////////// For SEO //////////////////////////////////////////////////
$navtitle = $row['pname'] . ' - ';
$metakeywords = $row['pname'] . lang("shop/template", 'SEO_gia_ban') . FormatNumber($row['end_price']) . $metakeywords;
$metadescription = trim($row['summary']);
$metadescription = strip_tags($metadescription);

/*
 * Check visit time left
 * return date/time
 * */
$visits = 0;
if (isset($_COOKIE["visits-$row[pid]"])) {
    $visits = (int)$_COOKIE["visits-$row[pid]"];
}else{
	setcookie("visits-$row[pid]", time(), time() + (60*60*24*7));
}

/**********************************************************************************************/
if ($row['sales'] > 0)
	$metadescription .= lang("shop/template", 'SEO_gia_da_giam') . $row['end_price'];

// Get images attachment of this product
$pro['img_cover'] = DB::fetch_first("SELECT aid, attachment
									 FROM " . DB::table('shop_products_attachment') . "
									 WHERE aid = " . $row['img_cover']);

$img_cover_for_seo = 'http://' . $_SERVER['HTTP_HOST'] . '/' . getattachURL($pro['img_cover'], 'shop/'); // for facebook
$img_cover = getattachURL($pro['img_cover'], 'shop/', 2); // Show in page
$fullSize = getattachURL($pro['img_cover'], 'shop/', 1); // Image full size
//////////////////////////////////////////IMAGES LIST/////////////////////////////////////////////////////
$images = implode(',', $row['images']);

if ($images){
	$img = true;
	$imgList = array();

	$query = DB::query("SELECT aid, remote, attachment
						FROM " . DB::table("shop_products_attachment") . "
						WHERE aid IN (" . $images . ")");
	unset($images);
	while ($attach = DB::fetch($query)){
		$row['galley'][$attach['aid']] = $attach;
		$row['galley'][$attach['aid']]['attdir'] = $attach['remote'] ? $_G['setting']['ftp']['attachurl'] . 'shop/' : $_G['setting']['attachurl'] . 'shop/';
//		$row['galley'][$attach['aid']]['folder'] = $row['images'][$attach['aid']]['attdir'] . substr($attach['attachment'], 0, 10);
	}

	foreach ($row['galley'] as $key=>$value){
		$imgList[] = $value['attdir'] . $value['attachment'];
	}
//	$imgList = array_unique($imgList);
}

///////////////////////////////////PRICE PRODUCT///////////////////////////////////
if ($row['sales'] > 0 && $row['status'] == 1){
	$row['sales_status'] = true;
	// Gia cua san pham
	$row['end_price'] = ($row['pprice'] - (doubleval($row['pprice']) * doubleval($row['sales'])) / 100);
	$row['sales'] = FormatNumber($row['sales'], 0, '.', ",", FALSE);
}else
	$row['end_price'] = $row['pprice'];

$row['price'] = FormatNumber($row['end_price'], 0, '.', ",");
$row['save_money'] = $row['pprice'] - $row['end_price'];
$row['pprice'] = FormatNumber($row['pprice']);

if ($row['save_money'] > 0)
	$save_money = FormatNumber($row['save_money']);

// Phi van chuyen
$delivery_price = $row['delivery_price'] > 0 ? '<small>+' . FormatNumber($row['delivery_price'], 0, '.', '') . '</small>' : '<span class="free">' . lang('shop/template', 'mien_phi') . '</span>';

// Thue ma san pham
$row['behind_tax'] = $row['tax']['used'] ? ($row['end_price'] + $row['delivery_price'] + ($row['end_price'] * $row['tax']['tax_vat']) / 100 + ($row['end_price'] * $row['tax']['tax_import']) / 100 + ($row['end_price'] * $row['tax']['tax_other']) / 100) : $row['end_price'];
$row['behind_tax'] = FormatNumber($row['behind_tax']);

// Detail product
if ($row['detail']){
	require_once libfile('function/discuzcode');
	$row['detail'] = discuzcode($row['detail'], 0, 0, 0, $_G['shop']['allowsmilies'], $_G['shop']['allowbbcode'], $_G['shop']['allowimgcode'], $_G['shop']['allowhtml'], 0, 0, 0, $_G['shop']['allowmediacode'], 0, $_G['setting']['lazyload']);
	$row['detail'] = paserimgatt($row['detail']);
}

// Thong so ky thuat cua san pham
if (!empty($_G['shop']['param_number'])){

	$sPram = $_G['shop']['param_number'];

	for ($i = 0; $i < $sPram; $i++){

		if (!empty($row['custom_' . $i])){
			$row['custom_' . $i] = unserialize($row['custom_' . $i]);

			foreach ($row['custom_' . $i] as $key=>$value){
				if ($key == 'name')
					$row['extra']['custom_' . $i]['name'] = $value;
				elseif ($key == 'custom'){
					if ($row['custom_' . $i]['custom']){
						$row['extra']['custom_' . $i]['custom'] = $value;
					}
				}
			}

			if ($row['custom_' . $i]['name']){
				unset($row['custom_' . $i]['name']);
				unset($row['custom_' . $i]['custom']);
				$row['extra']['custom_' . $i]['value'] = implode(', ', $row['custom_' . $i]);
			}

			unset($row['custom_' . $i]);
		}
	}

	// short property
	if ($sPram >= 4){
		$j=0;
		foreach ($row['extra'] as $key=>$value){
			$j++;

			$shortParam[] = array($value['name']=>$value['value']);

			if ($j == 4)
				break;
		}
	}

	/*
	 * For search
	 * get key 'custom_0' in array $row['extra']
	 * return array();
	 * */
	$param_title = $row['extra']['custom_0']['name'];
	$param_search = searchFirstKey($row['cid'], $row['pid'], $row['extra']['custom_0']);
	if (!empty($param_search)){
		$psearch = 1;
		$pResult = count($param_search);
//		var_dump($param_search);
	}
}

// Save history viewer
if ($_G['uid']){
	proViewCookie($row['pid']);
}

///////////////////////////////Get all comment of this product///////////////////////////////
$total_comment = getcount('home_comment', "id = " . $row['pid'] . " AND idtype='product'");
if ($total_comment <= 6)
	$pclimit = 2;
elseif ($total_comment > 6 && $total_comment <= 10)
	$pclimit = 6;
else
	$pclimit = 2;
$comments = proComment($row['pid'], $pclimit);

//////////////////////////San pham cung muc gia/////////////////////////////
$samePrice = samePrice($row['cid'], $row['pid'], $row['end_price'], 6);
if (!empty($samePrice)){
	$same = 1;
	$cat_same = count($samePrice);
//	var_dump($samePrice);
}

///////////////////////San pham cung danh muc///////////////////////////////
$inCategory = inCategory($row['cid'], $row['pid'], 20);
if (!empty($inCategory)){
	$inCat = 1;
	$cat_more = count($inCategory);
//	var_dump($inCategory);
}

///////////////////////Cac shop ban san pham nay////////////////////////////
$shopBuy = shopBuy($row['cid'], $row['pid'], $row['pname']);
if (!empty($shopBuy)){
	$buyer = 1;
	$all_buyer = count($shopBuy);
//	var_dump($shopBuy);
}

///////////////////////Cac san pham user nay dang ban////////////////////////
$sameShop = sameShop($row['uid'], $row['cid'], $row['pid'], 20);
if (!empty($sameShop)){
	$shoper = 1;
	//var_dump($sameShop);
}

// Khu vuc ban hang cua shop
$buyIn = '';
if ($row['store_province']){
	foreach ($provice as $key=>$value){
		if ($key == $row['store_province']){
			$buyIn = $value['name'];
		}
	}
}

/*
 * Check user like product
 * return true or false
 * */
$like = like::checkLike($row['pid'], 'pid');

/*
 * Share url
 * facebook
 * google plus
 * twitter
 * */
$canonical = $_G['siteurl'] . 'shop.php?mod=view&name=' . locdau($row['pname']) . '&pid=' . $row['pid'];
$shareUrl = urlencode($_G['siteurl'] . 'shop.php?mod=view&name=' . locdau($row['pname']) . '&pid=' . $row['pid']);
//////////////////////////////////////////////////////////////////////////
include template('shop/detail_product');
unset($row);